'use strict';
angular.module('app.controllers')
    .controller('HotelUserCtrl', function($scope, $state, $window, HrService, HotelService) {
        var vm = this;
        HotelService.getCurrentMonth().then(function(ret) {
            $window.sessionStorage.month = ret.data.value.month;
            $window.sessionStorage.year = ret.data.value.year;
            vm.month = $window.sessionStorage.month;
            $scope.loadProgress();

        })
        $scope.loadProgress = function() {
            HotelService.getProgress($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
                vm.progressRet = ret.data.value;

                vm.progress = {
                    turnovered: vm.progressRet.turnover1_post && vm.progressRet.turnover2_post && vm.progressRet.turnover3_post,
                    people: vm.progressRet.people_post,
                    recruitment: vm.progressRet.recruitment_post,
                    staffing: vm.progressRet.staffing1_post && vm.progressRet.staffing1_post,
                    pathway: vm.progressRet.pathway_post,
                    recognition: vm.progressRet.recognition_post,
                    directory: vm.progressRet.directory_post,
                    feedback: vm.progressRet.feedback_post,
                    external: vm.progressRet.external_post,
                    employee: vm.progressRet.employee_post,
                    manager: vm.progressRet.manager_post,
                    depart: vm.progressRet.depart_post && vm.progressRet.depart2_post,
                    cross: vm.progressRet.cross_post,
                    other: vm.progressRet.other_post,
                    budget: vm.progressRet.budget_post
                };
                var temp = 0;
                var all = 0;
                vm.progressNo = 0;
                angular.forEach(vm.progress, function(value, key) {
                    if (value == true) {
                        temp++;
                    }
                    all++;
                });
                vm.progressNo = (temp / all * 100).toFixed(2);
                vm.progressNoStyle = {"width": vm.progressNo + '%'};
            })
        }

        vm.userRole = $window.sessionStorage.userRole;


    })
    .controller('PeopleCtrl', function($scope, $state, $window, HrService, HotelService, ngDialog) {
        var vm = this;
        vm.diversityOptions = ['Foreign Expatriate', 'Local Expatriate', 'Local hire'];
        vm.loading = false;
        vm.loadData = function() {
            vm.loading = true;
            HrService.peopleGet($window.sessionStorage.hotelId).then(function(ret) {
                vm.gcPeople = ret.data.value.data;
                vm.lastUpdated = ret.data.value.lastUpdated;
                vm.lastUpdateBy = ret.data.value.lastUpdateBy;
                vm.loading = false;
            })
        }
        vm.loadData();

        vm.checkVacant = function(ret) {
            if (!ret.vacantDate || ret.vacantDate == '') {
                ret.vacantStatus = '';
            } else {
                var date = new Date(ret.vacantDate);
                ret.diversity = '';
                if ($window.sessionStorage.isTop == 'true') {
                    if ((new Date()) - date > 5184000000) { // 60 days
                        ret.vacantStatus = 'purple';
                    } else {
                        ret.vacantStatus = 'warning';
                    }
                } else {
                    if ((new Date()) - date > 7776000000) { // 90 days
                        ret.vacantStatus = 'danger';
                    } else {
                        ret.vacantStatus = 'warning';
                    }
                }
            }
            if (ret.comment == 'ok') {
                ret.vacantStatus = 'ok';
            }
        }

        vm.saveTable = function() {
            ngDialog.openConfirm({
                template: '\
                    <p>Are you sure you want to update GC People Planning data?</p>\
                    <div class="ngdialog-buttons">\
                        <button type="button" class="ngdialog-button ngdialog-button-secondary" ng-click="closeThisDialog(0)">No</button>\
                        <button type="button" class="ngdialog-button ngdialog-button-primary" ng-click="confirm(1)">Yes</button>\
                    </div>',
                plain: true
            }).then(function(value) {
                vm.loading = true;
                var valid = true;
                angular.forEach(vm.gcPeople, function(value, key) {
                    if ((value.name== null || value.name == ''|| value.name == ' ') && (value.vacantDate==null|| value.vacantDate == '' || !value.vacantDate)) {
                        vm.msg = 'ERROR: Name and Vacant Date can not be both empty!';
                        valid = false;
                    }
                    if ((value.name!=null && value.name.toUpperCase()== 'VACANT') && (value.vacantDate==null|| value.vacantDate == '' || !value.vacantDate)) {
                        vm.msg = 'ERROR: Please do not fill Vacant in the Name field, clear Name and select a Vacant Date!';
                        valid = false;
                    }
                });
                if(valid){
                    var sendBody = {
                    year: $window.sessionStorage.year,
                    month: $window.sessionStorage.month,
                    hotel: $window.sessionStorage.hotelId,
                    data: vm.gcPeople
                    }
                    HrService.peoplePost(sendBody).then(function(ret) {
                        if (ret.code == 'FAILED') {
                            alert(ret.reason);
                            vm.loading = false;
                            return false;
                        } else {
                            $state.go('home.hr.main', null, {
                                reload: true
                            });
                            vm.msg = 'Saved. ' + new Date();
                        }
                    })
                }
                
            }, function(value) {
                return;
            });

            // if(!confirm('Are you sure you want to update GC People Planning data?')){
            //     return;
            // }


        }
    })
    .controller('HrMainCtrl', function($scope, $state, ngDialog, $window, HrService) {

        var vm = this;

        //vm.result = [{band:'Band1'}, {band: 'Band2'}, {band: 'Band3'}, {band: 'Band4'}, {band: 'Band5'}, {band: 'Band6'}, {band: 'Band7'}];
        vm.editable = $window.sessionStorage.editable == 'true';
        vm.brand = $window.sessionStorage.brand;
        $scope.$state = $state;
        vm.loading = false;
        vm.loadData = function() {
            vm.loading = true;
            HrService.turnoverDepartGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
                vm.result = ret.data.value.list;
                vm.totalSum = ret.data.value.total;
                vm.calculateBandTotal();
                 HrService.turnoverLeaderGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
                    vm.result2 = ret.data.value.list;
                    HrService.turnoverInterviewGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
                        var tmpRet = ret.data.value.list;
                        angular.forEach(tmpRet, function(value, key) {
                            if (value.area == 'job') {
                                vm.result3 = value.data;
                            } else if (value.area == 'personal') {
                                vm.result4 = value.data;
                            } else if (value.area == 'other') {
                                vm.result5 = value.data;
                            }
                        })
                        vm.loading = false;
                    })
                });
            });
        }
        vm.loadData();
        vm.saveTable = function() {
            vm.loading = true;
            console.log(vm.result);
            vm.sendBody = {
                "year": $window.sessionStorage.year,
                "month": $window.sessionStorage.month,
                "hotel": $window.sessionStorage.hotelId, //hotel id
                "total": vm.totalSum,
                "list": vm.result
            };
            HrService.turnoverDepartSubmit(vm.sendBody).then(function(ret) {
                if (ret.code == 'FAILED') {
                    alert(ret.reason);
                    vm.loading = false;
                    return false;
                } else {
                    vm.loadData();
                    $scope.loadProgress();
                    vm.departMsg = 'Saved.' + new Date();
                }
            });
            $window.sessionStorage.turnovered = true;


        }
        vm.saveTurnoverLeaderTable = function() {
            vm.loading = true;
            vm.sendBody = {
                "year": $window.sessionStorage.year,
                "month": $window.sessionStorage.month,
                "hotel": $window.sessionStorage.hotelId, //hotel id

                "list": vm.result2
            };
            HrService.turnoverLeaderSubmit(vm.sendBody).then(function(ret) {
                if (ret.code == 'FAILED') {
                    alert(ret.reason);
                    vm.loading = false;
                    return false;
                } else {
                    vm.loadData();
                    $scope.loadProgress();
                    vm.leaderMsg = 'Saved.' + new Date();
                }
            });
            $window.sessionStorage.turnovered = true;
        };
        vm.saveInterviewTable = function() {
            vm.loading = true;
            vm.sendBody = {
                "year": $window.sessionStorage.year,
                "month": $window.sessionStorage.month,
                "hotel": $window.sessionStorage.hotelId, //hotel id
                "list": [{
                    "area": "job",
                    "data": vm.result3
                }, {
                    "area": "personal",
                    "data": vm.result4
                }, {
                    "area": "other",
                    "data": vm.result5
                }]
            };
            HrService.turnoverInterviewSubmit(vm.sendBody).then(function(ret) {
                if (ret.code == 'FAILED') {
                    alert(ret.reason);
                    vm.loading = false;
                    return false;
                } else {
                    vm.loadData();
                    $scope.loadProgress();
                    vm.interviewMsg = 'Saved.' + new Date();
                }
            })
        }
        vm.calculateBandTotal = function() {
            vm.total = {
                band1: 0,
                band2: 0,
                band3: 0,
                band4: 0,
                band5: 0,
                band6: 0,
                band7: 0,
                band8: 0,
                band9: 0,
                band10: 0
            };
            angular.forEach(vm.result, function(value, key) {
                vm.total.band1 += parseInt(value.band1);
                vm.total.band2 += parseInt(value.band2);
                vm.total.band3 += parseInt(value.band3);
                vm.total.band4 += parseInt(value.band4);
                vm.total.band5 += parseInt(value.band5);
                vm.total.band6 += parseInt(value.band6);
                vm.total.band7 += parseInt(value.band7);
                vm.total.band8 += parseInt(value.band8);
                vm.total.band9 += parseInt(value.band9);
                vm.total.band10 += parseInt(value.band10);
            });
        };

        vm.calculate = function(ret) {
            vm.totalSum = 0;
            ret.total = parseInt(ret.band1) + parseInt(ret.band2) + parseInt(ret.band3) + parseInt(ret.band4) + parseInt(ret.band5) + parseInt(ret.band6) + parseInt(ret.band7) + parseInt(ret.band8) + parseInt(ret.band9) + parseInt(ret.band10);
            var tempTotal = 0;
            angular.forEach(vm.result, function(value, key) {
                if (value.depart == ret.depart) {
                    tempTotal += value.total;
                }
                vm.total.band1 += parseInt(value.band1);

            })
            ret.allTotal = tempTotal;
            angular.forEach(vm.result, function(value, key) {
                if (value.depart == ret.depart) {
                    value.allTotal = tempTotal;
                }
                vm.totalSum += parseInt(value.total != null ? value.total : 0);
            })
            vm.calculateBandTotal();

        }

        vm.calculateYtd = function(ret) {
            ret.ytd = parseInt(ret.ytdOrg) - parseInt(ret.mtdOrg) + parseInt(ret.mtd);
        }

    })

.controller('StaffingCtrl', function($scope, $state, $window, HrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.aeditable = $window.sessionStorage.aEditable == 'true';
    vm.loading = false;
    vm.loadData = function() {
        vm.loading = true;
        HrService.staffingTable1Get($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
            var data = ret.data.value;
            vm.staffing = data.staffing;
            vm.seperations = data.seperation;
            vm.others1 = data.others1;
            vm.others2 = data.others2;
            vm.seperation = data.seperationAll;
            vm.end = data.endOfMonth;
            vm.monthSum = data.monthSum;
            vm.getBeginMonth();
            vm.calculateSeperation();
            vm.calculateEndMonth();
            vm.calculateStaffMonth();
            HrService.staffingTable2Get($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
                vm.result2 = ret.data.value.data;
                 vm.loading = false;
            })
        });
       
    }
    vm.loadData();

    vm.calculateSeperation = function() {

        vm.seperation.band6.mtd = 0;
        vm.seperation.band6.ytd = 0;
        vm.seperation.band7.mtd = 0;
        vm.seperation.band7.ytd = 0;
        vm.seperation.all.mtd = 0;
        vm.seperation.all.ytd = 0;
        angular.forEach(vm.seperations, function(value, key) {
            vm.seperation.band6.mtd += parseInt(value.band6.mtd);
            vm.seperation.band6.ytd += parseInt(value.band6.ytd);
            vm.seperation.band7.mtd += parseInt(value.band7.mtd);
            vm.seperation.band7.ytd += parseInt(value.band7.ytd);
            vm.seperation.all.mtd += parseInt(value.all.mtd);
            vm.seperation.all.ytd += parseInt(value.all.ytd);
        });
    };

    vm.getBeginMonth = function() {
        angular.forEach(vm.staffing, function(value, key) {
            if (value.content == 'Beginning of this month') {
                vm.begin = value;
            }
            if (value.content == 'New Hires') {
                vm.newHire = value;
            }
            if (value.content == 'Transfer Ins') {
                vm.transferIn = value;
            }
        })
    }

    vm.calculateEndMonth = function() {
        vm.end.band6.mtd = 0;
        vm.end.band6.ytd = 0;
        vm.end.band7.mtd = 0;
        vm.end.band7.ytd = 0;
        vm.end.all.mtd = 0;
        vm.end.all.ytd = 0;
        vm.end.band6.mtdP = 0;
        vm.end.band6.ytdP = 0;
        vm.end.band7.mtdP = 0;
        vm.end.band7.ytdP = 0;
        vm.end.all.mtdP = 0;
        vm.end.all.ytdP = 0;

        vm.getBeginMonth();
        vm.end.band6.mtd = parseInt(vm.begin.band6.mtd) + parseInt(vm.newHire.band6.mtd) + parseInt(vm.transferIn.band6.mtd) - parseInt(vm.seperation.band6.mtd);
        // vm.end.band6.ytd = parseInt(vm.begin.band6.ytd) + parseInt(vm.newHire.band6.ytd) + parseInt(vm.transferIn.band6.ytd) - parseInt(vm.seperation.band6.ytd);
        vm.end.band6.ytd = (parseInt(vm.begin.band6.mtd) + vm.end.band6.mtd) / 2;
        vm.end.band7.mtd = parseInt(vm.begin.band7.mtd) + parseInt(vm.newHire.band7.mtd) + parseInt(vm.transferIn.band7.mtd) - parseInt(vm.seperation.band7.mtd);
        // vm.end.band7.ytd = parseInt(vm.begin.band7.ytd) + parseInt(vm.newHire.band7.ytd) + parseInt(vm.transferIn.band7.ytd) - parseInt(vm.seperation.band7.ytd);
        vm.end.band7.ytd = (parseInt(vm.begin.band7.mtd) + vm.end.band7.mtd) / 2;
        vm.end.all.mtd = parseInt(vm.begin.all.mtd) + parseInt(vm.newHire.all.mtd) + parseInt(vm.transferIn.all.mtd) - parseInt(vm.seperation.all.mtd);
        // vm.end.all.ytd = parseInt(vm.begin.all.ytd) + parseInt(vm.newHire.all.ytd) + parseInt(vm.transferIn.all.ytd) - parseInt(vm.seperation.all.ytd);
        vm.end.all.ytd = (parseInt(vm.begin.all.mtd) + vm.end.all.mtd) / 2;
        vm.end.band6.mtdP = (parseInt(vm.seperation.band6.mtd) / ((vm.end.band6.mtd + parseInt(vm.begin.band6.mtd)) / 2)) || 0;
        vm.end.band6.ytdP = (parseInt(vm.seperation.band6.ytd) / ((vm.end.band6.mtd + parseInt(vm.begin.band6.ytd)) / 2)) || 0;
        vm.end.band7.mtdP = (parseInt(vm.seperation.band7.mtd) / ((vm.end.band7.mtd + parseInt(vm.begin.band7.mtd)) / 2)) || 0;
        vm.end.band7.ytdP = (parseInt(vm.seperation.band7.ytd) / ((vm.end.band7.mtd + parseInt(vm.begin.band7.ytd)) / 2)) || 0;
        vm.end.all.mtdP = (parseInt(vm.seperation.all.mtd) / ((vm.end.all.mtd + parseInt(vm.begin.all.mtd)) / 2)) || 0;
        vm.end.all.ytdP = (parseInt(vm.seperation.all.ytd) / ((vm.end.all.mtd + parseInt(vm.begin.all.ytd)) / 2)) || 0;

    };

    vm.calculateStaffMonth = function() {
        vm.monthSum.band6.mtd = 0;
        vm.monthSum.band7.mtd = 0;
        vm.monthSum.all.mtd = 0;

        angular.forEach(vm.others2, function(value, key) {
            vm.monthSum.band6.mtd += parseInt(value.band6.mtd);
            vm.monthSum.band7.mtd += parseInt(value.band7.mtd);
            vm.monthSum.all.mtd += parseInt(value.all.mtd);
        })
        vm.monthSum.band6.mtd += parseInt(vm.end.band6.mtd);
        vm.monthSum.band7.mtd += parseInt(vm.end.band7.mtd);
        vm.monthSum.all.mtd += parseInt(vm.end.all.mtd);

        angular.forEach(vm.others1, function(value, key) {
            if (value.content == 'Transfer to sister hotel(s)') {
                vm.monthSum.band6.mtd -= parseInt(value.band6.mtd);
                vm.monthSum.band7.mtd -= parseInt(value.band7.mtd);
                vm.monthSum.all.mtd -= parseInt(value.all.mtd);
            }
        })

    }

    vm.calculateYtd = function(ret) {
        ret.all.mtd = parseInt(ret.band6.mtd) + parseInt(ret.band7.mtd);
        if ((ret.content == 'Beginning of this month' && $window.sessionStorage.month == 1) || ret.content != 'Beginning of this month') {
            ret.band6.ytd = parseInt(ret.band6.ytdOrg) - parseInt(ret.band6.mtdOrg) + parseInt(ret.band6.mtd);
            ret.band7.ytd = parseInt(ret.band7.ytdOrg) - parseInt(ret.band7.mtdOrg) + parseInt(ret.band7.mtd);
        }
        if(ret.content == 'Manning budget of this year'){
            ret.band6.ytd = parseInt(ret.band6.mtd);
            ret.band7.ytd = parseInt(ret.band7.mtd);
        }

        ret.all.ytd = parseInt(ret.band6.ytd) + parseInt(ret.band7.ytd);
        vm.calculateSeperation();
        vm.calculateEndMonth();
        vm.calculateStaffMonth();
    }
    vm.calculateOthers1 = function(ret) {
        vm.calculateYtd(ret);
        ret.band6.mtdP = (parseInt(ret.band6.mtd) / parseInt(vm.end.band6.mtd));
        ret.band6.ytdP = (parseInt(ret.band6.ytd) / parseInt(vm.end.band6.ytd));
        ret.band7.mtdP = (parseInt(ret.band7.mtd) / parseInt(vm.end.band7.mtd));
        ret.band7.ytdP = (parseInt(ret.band7.ytd) / parseInt(vm.end.band7.ytd));
        ret.all.mtdP = (parseInt(ret.all.mtd) / parseInt(vm.end.all.mtd));
        ret.all.ytdP = (parseInt(ret.all.ytd) / parseInt(vm.end.all.ytd));

    };

    vm.saveTable = function() {
        vm.loading = true;
        var sendBody = {
            year: $window.sessionStorage.year,
            month: $window.sessionStorage.month,
            hotel: $window.sessionStorage.hotelId,
            staffing: vm.staffing,
            seperation: vm.seperations,
            others1: vm.others1,
            others2: vm.others2,
            seperationAll: vm.seperation,
            endOfMonth: vm.end,
            monthSum: vm.monthSum
        };
        if (!vm.staffing[0].band7.mtd || vm.staffing[0].band7.mtd == '') {
            vm.band7 = true;
            return;
        } else if (!vm.staffing[0].band6.mtd || vm.staffing[0].band6.mtd == '') {
            vm.band6 = true;
            return;
        } else {
            vm.band7 = false;
            vm.band6 = false;
        }

        HrService.turnoverDepartGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {

            vm.totalSum = ret.data.value.total;
            if (vm.totalSum != vm.seperation.all.mtd) {
                vm.errorMsg = ' ERROR: Seperation Overall MTD No. is not consist with Turnover by depart total No.!';
                vm.error = true;
                //alert('Seperation Overall MTD No. is not consist with Turnover by depart total No.');
               
            } else {
                vm.errorMsg = null;
                vm.error = false;
            }
            HrService.staffingTable1Post(sendBody).then(function(ret) {
                    if (ret.code == 'FAILED') {
                        vm.msg = ret.reason;
                        vm.loading = false;
                        return false;
                    } else {
                        var sendBody2 = {
                            year: $window.sessionStorage.year,
                            month: $window.sessionStorage.month,
                            hotel: $window.sessionStorage.hotelId,
                            data: vm.result2
                        };
                        HrService.staffingTable2Post(sendBody2).then(function(ret) {
                            if (ret.code == 'FAILED') {
                                alert(ret.reason);
                                vm.loading = false;
                                return false;
                            } else {
                                vm.loadData();
                                $scope.loadProgress();
                                vm.msg = 'Saved.' + new Date();
                                vm.loading = false;
                            }
                        })
                    }
                })
        });



    };

    vm.calculateYtd2 = function(ret) {
        ret.ytd = parseFloat(ret.ytdOrg) - parseFloat(ret.mtdOrg) + parseFloat(ret.mtd);
    }



})

.controller('RecruitCtrl', function($scope, $state, ngDialog, $window, HrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;
    vm.loadData = function() {
        vm.loading = true;
        HrService.recruimentGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
            vm.result = ret.data.value.data;
            angular.forEach(vm.result, function(value, key) {
                if (value.type == 'No. of employess recruited are the ones who return to their hometown') {
                    value.no = true;
                }
            })
            vm.loading=false;
        })
    }
    vm.loadData();

    vm.calculateYtd = function(ret) {
        ret.aytd = parseInt(ret.aytdOrg) - parseInt(ret.amtdOrg) + parseInt(ret.amtd);
        ret.nytd = parseInt(ret.nytdOrg) - parseInt(ret.nmtdOrg) + parseInt(ret.nmtd);
    };

    vm.saveTable = function() {
        vm.loading=true;
        angular.forEach(vm.result, function(value, key) {
            if (value.no) {
                delete value.no;
            }
        });
        var sendBody = {
            hotel: $window.sessionStorage.hotelId,
            year: $window.sessionStorage.year,
            month: $window.sessionStorage.month,
            data: vm.result
        }
        HrService.recruimentPost(sendBody).then(function(ret) {
            if (ret.code == 'FAILED') {
                alert(ret.reason);
                vm.loading = false;
                return false;
            } else {
                vm.loadData();
                $scope.loadProgress();
                vm.msg = 'Saved.' + new Date();
                $window.sessionStorage.canReport = true;
            }
        })
    }



})

.controller('PathwayCtrl', function($scope, $state, $window, HrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading=false;
    vm.loadData = function() {
        vm.loading=true;
        HrService.pathwayGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
            vm.result = ret.data.value.list;
            vm.loading=false;
        })
    }
    vm.loadData();
    vm.saveTable = function() {
        vm.loading=true;
        var sendBody = {
            hotel: $window.sessionStorage.hotelId,
            year: $window.sessionStorage.year,
            month: $window.sessionStorage.month,
            list: vm.result
        }
        HrService.pathwayPost(sendBody).then(function(ret) {
            if (ret.code == 'FAILED') {
                alert(ret.reason);
                vm.loading = false;
                return false;
            } else {
                vm.loadData();
                $scope.loadProgress();
                vm.msg = 'Saved.' + new Date();
            }
        })
    }

    vm.calculateYtd = function(item, ret) {
        // vm.result[4].data[0].mtd = 0;
        // vm.result[4].data[1].mtd = 0;
        // vm.result[4].data[2].mtd = 0;
        // vm.result[4].data[3].mtd = 0;
        // vm.result[4].data[4].mtd = 0;
        // vm.result[4].data[5].mtd = 0;
        // vm.result[0].data[0].ytd = 0;
        // vm.result[0].data[1].ytd = 0;
        // vm.result[0].data[2].ytd = 0;
        // vm.result[0].data[3].ytd = 0;
        // vm.result[0].data[4].ytd = 0;
        // vm.result[0].data[5].ytd = 0;
        // item.ytd = parseInt(item.ytdOrg) - parseInt(item.mtdOrg) + parseInt(item.mtd);

        angular.forEach(vm.result, function(value, key) {
            value.data[1].ytdP = parseInt(value.data[1].ytd) / parseInt(value.data[0].ytd) * 100 || 0;
            value.data[2].ytdP = parseInt(value.data[2].ytd) / parseInt(value.data[0].ytd) * 100 || 0;
            value.data[3].ytdP = parseInt(value.data[3].ytd) / parseInt(value.data[0].ytd) * 100 || 0;
            value.data[4].ytdP = parseInt(value.data[4].ytd) / parseInt(value.data[0].ytd) * 100 || 0;
            value.data[5].ytdP = parseInt(value.data[5].ytd) / parseInt(value.data[0].ytd) * 100 || 0;

            if (value.area != 'total') {

                // vm.result[4].data[0].mtd += parseInt(value.data[0].mtd);
                // vm.result[4].data[1].mtd += parseInt(value.data[1].mtd);
                // vm.result[4].data[2].mtd += parseInt(value.data[2].mtd);
                // vm.result[4].data[3].mtd += parseInt(value.data[3].mtd);
                // vm.result[4].data[4].mtd += parseInt(value.data[4].mtd);
                // vm.result[4].data[5].mtd += parseInt(value.data[5].mtd);

                // vm.result[0].data[0].ytd += parseInt(value.data[0].ytd);
                // vm.result[0].data[1].ytd += parseInt(value.data[1].ytd);
                // vm.result[0].data[2].ytd += parseInt(value.data[2].ytd);
                // vm.result[0].data[3].ytd += parseInt(value.data[3].ytd);
                // vm.result[0].data[4].ytd += parseInt(value.data[4].ytd);
                // vm.result[0].data[5].ytd += parseInt(value.data[5].ytd);

                vm.result[0].data[1].ytdP = (parseInt(vm.result[0].data[1].ytd) / parseInt(vm.result[0].data[0].ytd) * 100) || 0;
                vm.result[0].data[2].ytdP = (parseInt(vm.result[0].data[2].ytd) / parseInt(vm.result[0].data[0].ytd) * 100) || 0;
                vm.result[0].data[3].ytdP = (parseInt(vm.result[0].data[3].ytd) / parseInt(vm.result[0].data[0].ytd) * 100) || 0;
                vm.result[0].data[4].ytdP = (parseInt(vm.result[0].data[4].ytd) / parseInt(vm.result[0].data[0].ytd) * 100) || 0;
                vm.result[0].data[5].ytdP = (parseInt(vm.result[0].data[5].ytd) / parseInt(vm.result[0].data[0].ytd) * 100) || 0;

            }

        })
    };
})


.controller('RecognitionCtrl', function($scope, $state, $window, HrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;
    vm.loadData = function() {
        vm.loading=true;
        HrService.recognitionGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
            vm.result = ret.data.value.data;
            vm.loading=false;
        })
    }
    vm.loadData();

    vm.calculate = function(ret) {
        ret.ytd = parseInt(ret.ytdOrg) - parseInt(ret.mtdOrg) + parseInt(ret.mtd);
    };
    vm.saveTable = function() {
        vm.loading=true;
        var sendBody = {
            hotel: $window.sessionStorage.hotelId,
            year: $window.sessionStorage.year,
            month: $window.sessionStorage.month,
            data: vm.result
        }
        HrService.recognitionPost(sendBody).then(function(ret) {
            if (ret.code == 'FAILED') {
                alert(ret.reason);
                vm.loading = false;
                return false;
            } else {
                vm.loadData();
                $scope.loadProgress();
                vm.msg = 'Saved.' + new Date();
            }
        })
    }

})


.controller('DirectoryCtrl', function($scope, $state, $window, HrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading= false;
    vm.msg = '';
    vm.form = {
        hr: {},
        tr: {}
    };
    vm.loadData = function() {
        vm.loading=true;
        HrService.directoryGet($window.sessionStorage.hotelId).then(function(ret) {
            vm.form = ret.data.value;
            if (vm.form.hr == null) {
                vm.form.hr = {}
            }
            if (vm.form.tr == null) {
                vm.form.tr = {}
            }

            vm.loading=false;
        })
    }
    $scope.dateOptions.initDate = new Date('1985-01-01')
    vm.loadData();

    vm.saveTable = function() {
        vm.loading=true;
        vm.form.year = $window.sessionStorage.year,
            vm.form.month = $window.sessionStorage.month,
            vm.form.hotel = $window.sessionStorage.hotelId;

        if (!vm.form.hr.name || vm.form.hr.name == '') {
            $scope.hrname = true;
            return;
        } else if (!vm.form.tr.name || vm.form.tr.name == '') {
            $scope.trname = true;
            return;
        } else if (!vm.form.hr.position || vm.form.hr.position == '') {
            $scope.hrposition = true;
            return;
        } else if (!vm.form.tr.position || vm.form.tr.position == '') {
            $scope.trposition = true;
            return;
        } else if (!vm.form.hr.oline || vm.form.hr.oline == '') {
            $scope.hroline = true;
            return;
        } else if (!vm.form.tr.oline || vm.form.tr.oline == '') {
            $scope.troline = true;
            return;
        } else if (!vm.form.hr.mobile || vm.form.hr.mobile == '') {
            $scope.hrmobile = true;
            return;
        } else if (!vm.form.tr.mobile || vm.form.tr.mobile == '') {
            $scope.trmobile = true;
            return;
        } else if (!vm.form.hr.email || vm.form.hr.email == '') {
            $scope.hremail = true;
            return;
        } else if (!vm.form.tr.email || vm.form.tr.email == '') {
            $scope.tremail = true;
            return;
        } else if (!vm.form.hr.birthday || vm.form.hr.birthday == '') {
            $scope.hrbday = true;
            return;
        } else if (!vm.form.tr.birthday || vm.form.tr.birthday == '') {
            $scope.trbday = true;
            return;
        } else if (!vm.form.hotelName || vm.form.hotelName == '') {
            $scope.hotelname = true;
            return;
        } else if (!vm.form.hotelAddress || vm.form.hotelAddress == '') {
            $scope.hoteladd = true;
            return;
        } else if (!vm.form.postal || vm.form.postal == '') {
            $scope.hotelpost = true;
            return;
        } else {
            $scope.hrname = false;
            $scope.trname = false;
            $scope.hrposition = false;
            $scope.trposition = false;
            $scope.hroline = false;
            $scope.troline = false;
            $scope.hrmobile = false;
            $scope.trmobile = false;
            $scope.hremail = false;
            $scope.tremail = false;
            $scope.hrbday = false;
            $scope.trbday = false;
            $scope.hotelname = false;
            $scope.hoteladd = false;
            $scope.hotelpost = false;
        }
        HrService.directoryPost(vm.form).then(function(ret) {
            vm.msg = 'Saved. ' + new Date();
            vm.loadData();
            $scope.loadProgress();
        })
    }
})

.controller('FeedbackCtrl', function($scope, $state, $window, HrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading=false;
    vm.msg = '';
    vm.loadData = function() {
        vm.loading=true;
        HrService.feedbackGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
            vm.form = ret.data.value;
            vm.loading=false;
        })
    }

    vm.loadData();

    vm.saveTable = function() {
        vm.loading=true;
        vm.form.year = $window.sessionStorage.year,
            vm.form.month = $window.sessionStorage.month,
            vm.form.hotel = $window.sessionStorage.hotelId;
        HrService.feedbackPost(vm.form).then(function(ret) {
            vm.msg = 'Saved. ' + new Date();
            vm.loadData();
            $scope.loadProgress();
        })
    }
})